package com.kma.ncpractice2013.dao;

import com.kma.ncpractice2013.model.Order;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.RowMapper;

/**
 *
 * @author Illya
 */
public class OrderDAO extends DAO<Order>
{
    
    private final String SQL_INSERT_QUERY = "INSERT INTO USERS(ID,USER_ID,LOCATION_ID,ROOMS,ADULTS,CHILDREN,"
            + "NIGHTS,HOTEL_STARS,PRICE,ORDER_DATE,CHECK_IN_DATE,CHECK_OUT_DATE)" +
                "VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
    private final String SQL_UPDATE_QUERY = "UPDATE USERS SET USER_ID = ?,LOCATION_ID = ?,ROOMS = ?,ADULTS = ?,"
                + "CHILDREN = ?,NIGHTS = ?,HOTEL_STARS = ?,PRICE = ?,ORDER_DATE = ?,"
                + "CHECK_IN_DATE = ?,CHECK_OUT_DATE = ? WHERE ID = ?";
    
    public void create(Order order)
    {
        String sql = SQL_INSERT_QUERY;
        jdbcTemplate.update(sql, new Object[]{order.getId(),order.getUserId(),
            order.getLocationId(),order.getRooms(),order.getAdults(),order.getChildren(),order.getNights(),
            order.getHotelStars(),order.getPrice(),order.getOrderDate(),order.getCheckInDate(),
            order.getCheckOutDate()});
    }

    public void update(Order order)
    {
        String sql = SQL_UPDATE_QUERY;
        jdbcTemplate.update(sql, new Object[]{order.getUserId(),order.getLocationId(),
            order.getRooms(),order.getAdults(),order.getChildren(),order.getNights(),
            order.getHotelStars(),order.getPrice(),order.getOrderDate(),order.getCheckInDate(),
            order.getCheckOutDate()});
    }

    public Order getById(int id)
    {
        List<Order> orders;
        String sql = "SELECT * FROM ORDERS WHERE ID = " + id;
        orders = jdbcTemplate.query(sql, new OrderRowMapper());
        return orders.get(0);
    }

    public List<Order> getAll()
    {
        String sql = "SELECT  * FROM ORDERS ";
        List<Order> orders = jdbcTemplate.query(sql, new OrderRowMapper());
        return orders;
    }
	public List<Order> getByUser(int userId)
	{
		String sql = "SELECT * FROM ORDERS WHERE USER_ID = ?";
		List<Order> orders = jdbcTemplate.query(sql, new OrderRowMapper(), userId);
		return orders;
	}

    public void deleteById(int id)
    {
        String sql = "DELETE FROM ORDERS WHERE ID = " + id;
        jdbcTemplate.update(sql);
    }

    private static final class OrderRowMapper implements RowMapper
    {
        @Override
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException
        {
            Order order = new Order();
            order.setId(rs.getInt("id"));
            order.setAdults(rs.getInt("adults"));
            order.setCheckInDate(rs.getString("check_in_date"));
            order.setCheckOutDate(rs.getString("check_out_date"));
            order.setChildren(rs.getInt("children"));
            order.setHotelStars(rs.getInt("stars"));
            order.setLocationId(rs.getInt("location_id"));
            order.setNights(rs.getInt("nights"));
            order.setOrderDate(rs.getDate("date_of_order"));
            order.setPrice(rs.getDouble("price"));
            order.setRooms(rs.getInt("rooms"));
            order.setUserId(rs.getInt("USER_ID"));
	        order.setConfirmationToken(rs.getString("confirmation_token"));
	        order.setConfirmed(rs.getInt("confirmed"));
            return order;
        }
    }
}
